<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Generates randomness</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.random-engine.html">« Random\Engine</a></li>
      <li style="float: right;"><a href="class.random-engine-mt19937.html">Random\Engine\Mt19937 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.random-engine.html">Random\Engine</a></li>
    <li>Generates randomness</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="random-engine.generate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Random\Engine::generate</h1>
  <p class="verinfo">(PHP 8 &gt;= 8.2.0)</p><p class="refpurpose"><span class="refname">Random\Engine::generate</span> &mdash; <span class="dc-title">Generates randomness</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-random-engine.generate-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Random\Engine::generate</strong></span>(): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   Returns randomness and advances the algorithm’s state by one step.
  </p>
  <p class="para">
   The randomness is represented by a binary string containing random bytes. This representation
   allows to unambiguously interpret the random bits generated by the algorithm, for example to
   accomodate different output sizes used by different algorithms.
  </p>
  <p class="para">
   Algorithms that natively operate on integer values should return the integer in little-endian
   byte order, for example by leveraging the <span class="function"><a href="function.pack.html" class="function">pack()</a></span> function with the
   <code class="literal">P</code> format code. The high-level interface provided by the
   <span class="classname"><a href="class.random-randomizer.html" class="classname">Random\Randomizer</a></span> will interpret the returned random bytes as unsigned
   little-endian integers if a numeric representation is required.
  </p>
  <p class="para">
   It is strongly recommended that each bit of the returned string is uniformly and independently
   selected, as some applications require randomness based on the bit-level to work correctly.
   For example linear congruential generators often generate lower-quality randomness for the less
   significant bits of the return integer value and thus would not be appropriate for applications
   that require bit-level randomness.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-random-engine.generate-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-random-engine.generate-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   A non-empty string containing random bytes.
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    The <span class="classname"><a href="class.random-randomizer.html" class="classname">Random\Randomizer</a></span> works with unsigned 64 bit integers internally.
    If the returned string contains more than 64 bit (8 byte) of randomness the exceeding
    bytes will be ignored. Other applications may be able to process more than 64 bit at once.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 errors" id="refsect1-random-engine.generate-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     If generating randomness fails, a <span class="classname"><a href="class.random-randomexception.html" class="classname">Random\RandomException</a></span> should
     be thrown. Any other <span class="classname"><a href="class.exception.html" class="classname">Exception</a></span> thrown during generation should
     be caught and wrapped into a <span class="classname"><a href="class.random-randomexception.html" class="classname">Random\RandomException</a></span>.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If the returned string is empty, a <span class="classname"><a href="class.random-brokenrandomengineerror.html" class="classname">Random\BrokenRandomEngineError</a></span>
     will be thrown by the <span class="classname"><a href="class.random-randomizer.html" class="classname">Random\Randomizer</a></span>.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If the implemented algorithm is severely biased, a <span class="classname"><a href="class.random-brokenrandomengineerror.html" class="classname">Random\BrokenRandomEngineError</a></span>
     may be thrown by the <span class="classname"><a href="class.random-randomizer.html" class="classname">Random\Randomizer</a></span> to prevent infinite loops
     if rejection sampling is required to return unbiased results.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-random-engine.generate-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-3488">
   <p><strong>示例 #1 <span class="function"><strong>Random\Engine::generate()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;...&nbsp;*/<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
...
</pre></div>
   </div>
  </div>
 </div>



</div></div></div></body></html>